<?php namespace app\home\controller;

use houdunwang\view\View;
use system\model\Category;

class ListPage extends Common {
    //动作
    public function index(){
         $headConf =  ['title'=>'媒体博客--列表页'];
         View::with('headConf',$headConf);

         //var_dump($_GET);
        $tid=q('get.tid');
        $cid=q('get.cid');
        View::with('tid',$tid);
        View::with('cid',$cid);
        if($cid){
            //说明是点击分类过来的
            //1.获取当前分类所有子集数据(包含子集)
            //获取当前分类的所有子集数据
            $cids=(new Category())->getSon(Db::table('category')->get(),$cid);
            $cids[]=$cid;
            //p($cids);
            //标签：分类或标签名称
            $header = [ 'name'=>'分类',
                        'value'=> Db::table('category')->where('cid',$cid)->pluck('cname'),
                        'count'=>Db::table('article')->where('is_recycle',0)->whereIn('category_cid',$cids)->count(),
            ];
            View::with('header',$header);
            //文章数据
            $articleData = Db::table('article')->where('is_recycle',0)->join('category','category_cid','=','cid')
                ->whereIn('category_cid',$cids)
                ->field('aid,title,author,sendtime,category_cid,cname,digest,thumb')->get();
            foreach($articleData as $k=>$v){
                //$k=0的作为下面关联表的过滤对象，再$k=1的做……，$=2的做……，但必须是追加进数据，否则每次覆盖$s
                $articleData[$k]['ofTag'] =Db::table('article_tag')->join('tag','tag_tid','=','tid')
                    ->where('article_aid',$v['aid'])->lists('tid,tname');

            }
            View::with('articleData',$articleData);
            //p($articleData);
        }

        if($tid){
            //说明是点击标签进来的
            $header = [
                'name'=>'标签',
                'value'=>Db::table('tag')->where('tid','=',$tid)->pluck('tname'),
                'count'=>Db::table('article_tag')->join('article','article_aid','=','aid')->where('is_recycle',0)->where('tag_tid',$tid)->count(),
            ];
            View::with('header',$header);

            //文章数据
            $articleData = Db::table('article')->join('article_tag','aid','=','article_aid')->where('is_recycle',0)
                ->where('tag_tid',$tid)->join('category','category_cid','=','cid')
                ->field('aid,title,sendtime,thumb,digest,author,tag_tid,cid,cname')->get();
            //p($articleData);
            foreach ($articleData as $k=>$v){
                $articleData[$k]['ofTag']=Db::table('article_tag')->join('tag','tag_tid','=','tid')->where('article_aid',$v['aid'])->lists('tid,tname');
            }
            //p($articleData);
            View::with('articleData',$articleData);
        }
        return view();
    }
}















